package com.example.bleuesprit.parkAndroidClient.net;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;

public class ExampleClient extends WebSocketClient {

    Handler handler;


    public ExampleClient( URI serverUri , Draft draft,Handler handler) {
        super( serverUri, draft );
        this.handler=handler;
    }



    @Override
    public void onOpen( ServerHandshake handshakedata ) {
        Log.i("websocket","opened connection");
        // if you plan to refuse connection based on ip or httpfields overload: onWebsocketHandshakeReceivedAsClient
    }

    @Override
    public void onMessage( String message ) {
        Log.i("websocket", "received: " + message);
        Message handlerMessage=new Message();
        handlerMessage.what=0x130;
        Bundle data=new Bundle();
        data.putString("result",message);
        handlerMessage.setData(data);
        handler.sendMessage(handlerMessage);
    }


    public void onFragment( Framedata fragment ) {
        System.out.println( "received fragment: " + new String( fragment.getPayloadData().array() ) );
    }

    @Override
    public void onClose( int code, String reason, boolean remote ) {
        // The codecodes are documented in class org.java_websocket.framing.CloseFrame
        Log.i("websocket","Connection closed by " + ( remote ? "remote peer" : "us" ));

    }

    @Override
    public void onError( Exception ex ) {
        ex.printStackTrace();
        // if the error is fatal then onClose will be called additionally
    }


}